﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BillsPresentation;
using BillsDomain;

namespace BillsWpf
{
    /// <summary>
    /// Interaction logic for AddPaymentWindow.xaml
    /// </summary>
    public partial class AddPaymentWindow : Window, IAddPaymentView
    {
        private AddPaymentPresenter presenter;

        public AddPaymentWindow()
        {
            InitializeComponent();
        }

        public AddPaymentWindow(IScheduledPaymentController scheduledPaymentController)
            : this()
        {
            this.presenter = new AddPaymentPresenter(this, scheduledPaymentController);
            this.datePickerStartDate.SelectedDate = DateTime.Today;
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        /// <summary>
        /// Gets a value indicating whether the payment is a new one or an existing one.
        /// </summary>
        /// <value><c>true</c> if adding a new payment; otherwise, <c>false</c>.</value>
        public bool AddMode { get; set; }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            if (this.SaveClicked != null)
            {
                this.SaveClicked(sender, e);
            }

            this.DialogResult = true;
        }

        #region IAddPaymentView Members

        /// <summary>
        /// Gets or sets the amount.
        /// </summary>
        /// <value>The amount.</value>
        public string Amount
        {
            get { return this.textBoxAmount.Text; }
            set { this.textBoxAmount.Text = value; }
        }

        /// <summary>
        /// Gets or sets the start date.
        /// </summary>
        /// <value>The start date.</value>
        public DateTime StartDate
        {
            get { return this.datePickerStartDate.SelectedDate.Value; }
            set { this.datePickerStartDate.SelectedDate = value; }
        }

        /// <summary>
        /// Gets the recurrence lookup list.
        /// </summary>
        /// <value>The recurrence lookup.</value>
        public ILookupList RecurrenceLookup
        {
            get { return new WpfLookupList(this.comboBoxRecurrance); }
        }

        /// <summary>
        /// Gets the payee name lookup list.
        /// </summary>
        /// <value>The payee name lookup list.</value>
        public ILookupList PayeeLookup
        {
            get { return new WpfLookupList(this.comboBoxPayee); }
        }

        /// <summary>
        /// Gets the type lookup list.
        /// </summary>
        /// <value>The type lookup.</value>
        public ILookupList TypeLookup
        {
            get { return new WpfLookupList(this.comboBoxType); }
        }

        /// <summary>
        /// Occurs when the Save button is pressed.
        /// </summary>
        public event EventHandler SaveClicked;

        /// <summary>
        /// Gets or sets a value indicating whether the payment should be included in balance projections.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if the payment should be included in balance projections; otherwise, <c>false</c>.
        /// </value>
        public bool IncludeInProjection
        {
            get { return this.checkBoxIncludeInProjection.IsChecked.Value; }
            set { this.checkBoxIncludeInProjection.IsChecked = value; }
        }

        #endregion

        #region IView Members

        /// <summary>
        /// Occurs when the window is loaded.
        /// </summary>
        public event EventHandler Load;

        /// <summary>
        /// Occurs when the window is closed.
        /// </summary>
        public event EventHandler FormClose;

        #endregion

        /// <summary>
        /// Handles the Loaded event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.Load != null)
            {
                this.Load(sender, e);
            }
        }

        /// <summary>
        /// Handles the Closed event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Window_Closed(object sender, EventArgs e)
        {
            if (this.FormClose != null)
            {
                this.FormClose(sender, e);
            }
        }
    }
}
